package han.yang.service.friendship.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import han.yang.common.ResponseVO;
import han.yang.service.friendship.dao.ImFriendShipGroupEntity;
import han.yang.service.friendship.dao.ImFriendShipGroupMemberEntity;
import han.yang.service.friendship.dao.mapper.ImFriendShipGroupMemberMapper;
import han.yang.service.friendship.model.req.AddFriendShipGroupMemberReq;
import han.yang.service.friendship.model.req.DeleteFriendShipGroupMemberReq;
import han.yang.service.friendship.service.ImFriendShipGroupMemberService;
import han.yang.service.friendship.service.ImFriendShipGroupService;
import han.yang.service.user.dao.ImUserDataEntity;
import han.yang.service.user.service.ImUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class ImFriendShipGroupMemberServiceImpl implements ImFriendShipGroupMemberService {

    @Resource
    ImFriendShipGroupService imFriendShipGroupService;

    @Resource
    ImUserService imUserService;

    @Resource
    ImFriendShipGroupMemberMapper imFriendShipGroupMemberMapper;

    @Transactional
    @Override
    public ResponseVO addGroupMember(AddFriendShipGroupMemberReq req) {
        // 查看分组是否存在
        ResponseVO<ImFriendShipGroupEntity> group = imFriendShipGroupService.getGroup(req.getFromId(),req.getGroupName(),req.getAppId());
        if(!group.isOk()){
            return group;
        }
        List<String> successId = new ArrayList<>();
        List<String> errorId = new ArrayList<>();
        for (String toId : req.getToIds()) {
            // 判断是否有该成员
            ResponseVO<ImUserDataEntity> singleUserInfo = imUserService.getSingleUserInfo(toId, req.getAppId());
            if(singleUserInfo.isOk()){
                int i = doAddGroupMember(group.getData().getGroupId(), toId);
                if(i == 1){
                    successId.add(toId);
                }else {
                    errorId.add(toId);
                }
            }else {
                errorId.add(toId);
            }
        }
        HashMap<String,List<?>> map = new HashMap<>();
        map.put("success",successId);
        map.put("error",errorId);
        return ResponseVO.successResponse(map);
    }

    @Override
    public ResponseVO<?> delGroupMember(DeleteFriendShipGroupMemberReq req) {
        ResponseVO<ImFriendShipGroupEntity> group = imFriendShipGroupService.getGroup(req.getFromId(), req.getGroupName(), req.getAppId());
        if (!group.isOk()){
            return group;
        }
        List<String> successId = new ArrayList<>();
        List<String> errorId = new ArrayList<>();
        req.getToIds().forEach(toId -> {
            ResponseVO<ImUserDataEntity> singleUserInfo = imUserService.getSingleUserInfo(toId, req.getAppId());
            if (singleUserInfo.isOk()){
                int i = doDeleteGroupMember(group.getData().getGroupId(),toId);
                if (i == 1){
                    // 删除成功
                    successId.add(toId);
                }else {
                    // 删除失败
                    errorId.add(toId);
                }
            }else {
                // 用户不存在
                errorId.add(toId);
            }
        });
        HashMap<String,List<?>> map = new HashMap<>();
        map.put("successIds",successId);
        map.put("errorIds",errorId);
        return ResponseVO.successResponse(map);
    }

    private int doDeleteGroupMember(Long groupId, String toId) {
        QueryWrapper<ImFriendShipGroupMemberEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("group_id",groupId);
        queryWrapper.eq("to_id",toId);
        try {
            int delete = imFriendShipGroupMemberMapper.delete(queryWrapper);
//            int insert = imFriendShipGroupMemberMapper.insert(imFriendShipGroupMemberEntity);
            return delete;
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public int daAddGroupMember(Long groupId, String toId) {

        ImFriendShipGroupMemberEntity imFriendShipGroupMemberEntity = new ImFriendShipGroupMemberEntity();
        imFriendShipGroupMemberEntity.setGroupId(groupId);
        imFriendShipGroupMemberEntity.setToId(toId);
        try {
            int insert = imFriendShipGroupMemberMapper.insert(imFriendShipGroupMemberEntity);
            return insert;
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public int clearGroupMember(Long groupId) {
        QueryWrapper<ImFriendShipGroupMemberEntity> query = new QueryWrapper<>();
        query.eq("group_id",groupId);
        int delete = imFriendShipGroupMemberMapper.delete(query);
        return delete;
    }

    @Override
    public int  doAddGroupMember(Long groupId, String toId) {
        ImFriendShipGroupMemberEntity imFriendShipGroupMemberEntity = new ImFriendShipGroupMemberEntity();
        imFriendShipGroupMemberEntity.setGroupId(groupId);
        imFriendShipGroupMemberEntity.setToId(toId);
        try {
            int insert = imFriendShipGroupMemberMapper.insert(imFriendShipGroupMemberEntity);
            return insert;
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
    }
}
